package com.kaifamiao.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import java.time.LocalDate;
import java.util.Set;
import com.kaifamiao.enums.Gender;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Table;
import javax.persistence.Enumerated;
import javax.persistence.EnumType;
import javax.persistence.OneToMany;
import javax.persistence.CascadeType;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;

@Data
@ToString(exclude = { "fileInfos" })
@EqualsAndHashCode(exclude = { "fileInfos" })
@Entity
@Table(name="t_users")
@DynamicInsert
@DynamicUpdate
public class User {

    @Id
    @GeneratedValue(generator="idGenerator")
    @GenericGenerator(name="idGenerator", strategy="increment")
    private Integer id;
    private String account; // 账号
    private String email; // 邮箱
    private String tel; // 手机号
    private String password; // 密码
    private String salt; // 盐值
    private String nickname; // 昵称
    @Enumerated(EnumType.STRING)
    private Gender gender; // 性别
    @Type(type = "LocalDate")
    private LocalDate birthdate; // 出生日期
    @Type(type = "boolean")
    private Boolean married; // 是否已婚
    @Type(type = "boolean")
    private Boolean admin; // 是否是管理员

    // 维护从 User 到 FileInfo 的 "一对多" 关联关系
    @OneToMany(mappedBy = "uploader", cascade= {CascadeType.PERSIST, CascadeType.REMOVE})
    @JsonIgnore
    private Set<FileInfo> fileInfos; // 表示当前用户上传的文件

}
